<template>
  <div class="ace-container">
    <!-- 官方文档中使用id，这里禁止使用，在后期打包后容易出现问题，使用 ref 或者 DOM 就行 -->
    <div ref="ace" class="ace-editor"></div>
  </div>
</template>

<script>
import ace from 'ace-builds'

import 'ace-builds/src-min-noconflict/theme-sqlserver'
// 设置主题
import themeSqlServerUrl from 'ace-builds/src-min-noconflict/theme-sqlserver.js?url'
ace.config.setModuleUrl('ace/theme/sqlserver', themeSqlServerUrl)

// 添加javascript语言模块
import javascriptModeUrl from 'ace-builds/src-min-noconflict/mode-javascript?url'
ace.config.setModuleUrl('ace/mode/javascript', javascriptModeUrl)

// 添加json语言模块
import jsonModeUrl from 'ace-builds/src-min-noconflict/mode-json?url'
ace.config.setModuleUrl('ace/mode/json', jsonModeUrl)

// 添加css语言模块
import cssModeUrl from 'ace-builds/src-min-noconflict/mode-css?url'
ace.config.setModuleUrl('ace/mode/css', cssModeUrl)

// 添加lua语言模块
import luaModeUrl from 'ace-builds/src-min-noconflict/mode-lua?url'
ace.config.setModuleUrl('ace/mode/lua', luaModeUrl)

// 用于完成语法检查、代码提示、自动补全等代码编辑功能
import workBaseUrl from 'ace-builds/src-min-noconflict/worker-base?url'
ace.config.setModuleUrl('ace/mode/base', workBaseUrl)

// 支持javascript
import javascriptWorkerUrl from 'ace-builds/src-min-noconflict/worker-javascript?url'
ace.config.setModuleUrl('ace/mode/javascript_worker', javascriptWorkerUrl)

// 支持json
import jsonWorkerUrl from 'ace-builds/src-min-noconflict/worker-json?url'
ace.config.setModuleUrl('ace/mode/json_worker', jsonWorkerUrl)

// 支持css
import cssWorkerUrl from 'ace-builds/src-min-noconflict/worker-css?url'
ace.config.setModuleUrl('ace/mode/css_worker', cssWorkerUrl)

// 支持lua
import luaWorkerUrl from 'ace-builds/src-min-noconflict/worker-lua?url'
ace.config.setModuleUrl('ace/mode/lua_worker', luaWorkerUrl)

// 加载javascript代码片段
import javascriptSnippetsUrl from 'ace-builds/src-min-noconflict/snippets/javascript?url'
ace.config.setModuleUrl('ace/snippets/javascript', javascriptSnippetsUrl)

// 加载json代码片段
import jsonSnippetsUrl from 'ace-builds/src-min-noconflict/snippets/json?url'
ace.config.setModuleUrl('ace/snippets/json', jsonSnippetsUrl)

// 加载css代码片段
import cssSnippetsUrl from 'ace-builds/src-min-noconflict/snippets/css?url'
ace.config.setModuleUrl('ace/snippets/css', cssSnippetsUrl)

// 加载lua代码片段
import luaSnippetsUrl from 'ace-builds/src-min-noconflict/snippets/lua?url'
ace.config.setModuleUrl('ace/snippets/lua', luaSnippetsUrl)

// 加载查找功能
import extSearchBox from 'ace-builds/src-min-noconflict/ext-searchbox?url'
ace.config.setModuleUrl('ace/ext/searchbox', extSearchBox)

// 加载语言工具
import 'ace-builds/src-min-noconflict/ext-language_tools'
ace.require('ace/ext/language_tools')

export default {
  name: 'CodeEditor',
  props: {
    modelValue: {
      type: String,
      default: ''
      //required: true
    },
    readonly: {
      type: Boolean,
      default: false
    },
    mode: {
      type: String,
      default: 'javascript'
    },
    userWorker: {
      //是否开启语法检查，默认开启
      type: Boolean,
      default: true
    },
    height: {
      type: Number,
      default: 460
    }
  },
  emits: ['update:modelValue'],
  data() {
    return {
      aceEditor: null,
      themePath: 'ace/theme/sqlserver',
      modePath: 'ace/mode/javascript',
      codeValue: this.modelValue
    }
  },
  watch: {
    //
  },
  mounted() {
    this.addAutoCompletion(ace) //添加自定义代码提示！！

    // let lines = Math.floor(this.height / 14)
    this.aceEditor = ace.edit(this.$refs.ace, {
      // maxLines: lines, // 最大行数，超过会自动出现滚动条
      // minLines: lines, // 最小行数，还未到最大行数时，编辑器会自动伸缩大小
      fontSize: 15, // 编辑器内字体大小
      theme: this.themePath, // 默认设置的主题
      mode: this.modePath, // 默认设置的语言模式
      tabSize: 4, // 制表符设置为4个空格大小
      readOnly: this.readonly,
      highlightActiveLine: true,
      value: this.codeValue,
      wrap: true,
      showPrintMargin: false
    })

    // 添加CSS样式来调整提示框宽度
    const style = document.createElement('style')
    style.innerHTML = `
      .ace_editor.ace_autocomplete {
        width: 468px !important;
      }
      .ace_content {
        width: 450px !important;
      }
      .ace_line {
        width: 450px !important;
      }
    `
    document.head.appendChild(style)

    this.aceEditor.setOptions({
      enableBasicAutocompletion: true,
      enableSnippets: true, // 设置代码片段提示
      enableLiveAutocompletion: true // 设置自动提示
    })

    if (this.mode === 'json') {
      this.setJsonMode()
    } else if (this.mode === 'css') {
      this.setCssMode()
    } else if (this.mode === 'lua') {
      this.setLuaMode()
    } else {
      this.setJsMode()
    }

    if (!this.userWorker) {
      this.aceEditor.getSession().setUseWorker(false)
    }

    //编辑时同步数据
    this.aceEditor.getSession().on('change', () => {
      this.$emit('update:modelValue', this.aceEditor.getValue())
    })
  },
  methods: {
    addAutoCompletion(ace) {
      let acData = [
        { meta: 'VForm API', caption: 'getWidgetRef', value: 'getWidgetRef()', score: 1 },
        { meta: 'VForm API', caption: 'getFormRef', value: 'getFormRef()', score: 1 },

        // Modbus APIs
    { meta: 'Modbus API', caption: 'modbus_get_regist', value: 'modbus_get_regist()', score: 10 },
    { meta: 'Modbus API', caption: 'modbus_get_regist_bit', value: 'modbus_get_regist_bit()', score: 10 },
    { meta: 'Modbus API', caption: 'modbus_regist_have_read', value: 'modbus_regist_have_read()', score: 6 },
    { meta: 'Modbus API', caption: 'modbus_write_one_regist', value: 'modbus_write_one_regist()', score: 11 },
    { meta: 'Modbus API', caption: 'modbus_write_one_regist_32bit', value: 'modbus_write_one_regist_32bit()', score: 9 },
    { meta: 'Modbus API', caption: 'modbus_write_one_regist_bit_buffer', value: 'modbus_write_one_regist_bit_buffer()', score: 9 },
    { meta: 'Modbus API', caption: 'modbus_write_muti_regist', value: 'modbus_write_muti_regist()', score: 9 },
    { meta: 'Modbus API', caption: 'modbus_write_coil', value: 'modbus_write_coil()', score: 7 },
    { meta: 'Modbus API', caption: 'modbus_connect_ok', value: 'modbus_connect_ok()', score: 8 },
    { meta: 'Modbus API', caption: 'modbus_set_cycle_info', value: 'modbus_set_cycle_info()', score: 7 },
    { meta: 'Modbus API', caption: 'modbus_read_regist_once', value: 'modbus_read_regist_once()', score: 7 },
    { meta: 'Modbus API', caption: 'modbus_read_regist_cycle', value: 'modbus_read_regist_cycle()', score: 7 },
    { meta: 'Modbus API', caption: 'modbus_clear_regist_cycle', value: 'modbus_clear_regist_cycle()', score: 7 },
    
    // User Data APIs
    { meta: 'User Data API', caption: 'get_user_data', value: 'get_user_data()', score: 11 },
    { meta: 'User Data API', caption: 'set_user_data', value: 'set_user_data()', score: 11 },
    { meta: 'User Data API', caption: 'get_user_data_32bit', value: 'get_user_data_32bit()', score: 9 },
    { meta: 'User Data API', caption: 'set_user_data_32bit', value: 'set_user_data_32bit()', score: 9 },
    { meta: 'User Data API', caption: 'save_user_data', value: 'save_user_data()', score: 1 },
    { meta: 'User Data API', caption: 'switch_user_data_to_char', value: 'switch_user_data_to_char()', score: 8 },
    { meta: 'User Data API', caption: 'switch_char_to_user_data', value: 'switch_char_to_user_data()', score: 8 },
    { meta: 'User Data API', caption: 'switch_hex_char_to_user_data', value: 'switch_hex_char_to_user_data()', score: 7 },
    { meta: 'User Data API', caption: 'get_user_data_hex_string', value: 'get_user_data_hex_string()', score: 7 },
    
    // UI Control APIs
    { meta: 'UI Control API', caption: 'create_new_page', value: 'create_new_page()', score: 10 },
    { meta: 'UI Control API', caption: 'get_page_paramter', value: 'get_page_paramter()', score: 1 },
    { meta: 'UI Control API', caption: 'refresh_cur_page', value: 'refresh_cur_page()', score: 1 },
    { meta: 'Control API', caption: 'on_control_notify', value: 'on_control_notify()', score: 1 },
    
    // Button APIs
    { meta: 'Button API', caption: 'create_button', value: 'create_button()', score: 1 },
    { meta: 'Button API', caption: 'update_button', value: 'update_button()', score: 9 },
    { meta: 'Button API', caption: 'set_long_press_time', value: 'set_long_press_time()', score: 5 },
    { meta: 'Button API', caption: 'event_send', value: 'event_send()', score: 1 },
    
    // Label APIs
    { meta: 'Label API', caption: 'create_label', value: 'create_label()', score: 1 },
    { meta: 'Label API', caption: 'update_label_text', value: 'update_label_text()', score: 11 },
    { meta: 'Label API', caption: 'create_border_label', value: 'create_border_label()', score: 1 },
    { meta: 'Label API', caption: 'set_label_line_space', value: 'set_label_line_space()', score: 1 },
    { meta: 'Label API', caption: 'set_label_text_color', value: 'set_label_text_color()', score: 1 },
    { meta: 'Label API', caption: 'set_label_text_font', value: 'set_label_text_font()', score: 1 },
    { meta: 'Label API', caption: 'set_label_text_font_language', value: 'set_label_text_font_language()', score: 1 },
    { meta: 'Label API', caption: 'set_label_attr', value: 'set_label_attr()', score: 1 },
    
    // Input APIs
    { meta: 'Input API', caption: 'update_input', value: 'update_input()', score: 9 },
    { meta: 'Input API', caption: 'update_input_title', value: 'update_input_title()', score: 8 },
    { meta: 'Input API', caption: 'update_input_language', value: 'update_input_language()', score: 1 },
    { meta: 'Input API', caption: 'update_input_special_value', value: 'update_input_special_value()', score: 1 },
    { meta: 'Input API', caption: 'update_user_data_callback', value: 'update_user_data_callback()', score: 1 },
    
    // Image APIs
    { meta: 'Image API', caption: 'update_png', value: 'update_png()', score: 10 },
    { meta: 'Image API', caption: 'set_png_width', value: 'set_png_width()', score: 8 },
    { meta: 'Image API', caption: 'set_png_height', value: 'set_png_height()', score: 8 },
    { meta: 'Image API', caption: 'add_png_buffer', value: 'add_png_buffer()', score: 6 },
    
    // Progress/Slider APIs
    { meta: 'Progress API', caption: 'set_bar_pos', value: 'set_bar_pos()', score: 1 },
    { meta: 'Progress API', caption: 'get_bar_pos', value: 'get_bar_pos()', score: 1 },
    { meta: 'Progress API', caption: 'set_bar_color', value: 'set_bar_color()', score: 1 },
    { meta: 'Progress API', caption: 'set_bar_range', value: 'set_bar_range()', score: 1 },
    { meta: 'Progress API', caption: 'set_slider_pos', value: 'set_slider_pos()', score: 1 },
    { meta: 'Progress API', caption: 'get_slider_pos', value: 'get_slider_pos()', score: 1 },
    { meta: 'Progress API', caption: 'get_slider_min_value', value: 'get_slider_min_value()', score: 1 },
    { meta: 'Progress API', caption: 'get_slider_max_value', value: 'get_slider_max_value()', score: 1 },
    { meta: 'Progress API', caption: 'slider_bind_label', value: 'slider_bind_label()', score: 1 },
    
    // Switch
    { meta: 'Switch API', caption: 'get_switch_status', value: 'get_switch_status()', score: 1 },
    { meta: 'Switch API', caption: 'set_switch_status', value: 'set_switch_status()', score: 1 },

    //LED APIs
    { meta: 'Led API', caption: 'set_led_color', value: 'set_led_color()', score: 1 },
    
    // QR Code/GIF APIs
    { meta: 'QR Code API', caption: 'update_qr_code', value: 'update_qr_code()', score: 1 },
    { meta: 'GIF API', caption: 'stop_gif', value: 'stop_gif()', score: 1 },
    { meta: 'GIF API', caption: 'stop_png_gif', value: 'stop_png_gif()', score: 1 },
    
    // Combobox/Grid APIs
    { meta: 'Combobox API', caption: 'add_combobox_string', value: 'add_combobox_string()', score: 1 },
    { meta: 'Combobox API', caption: 'add_combobox_png', value: 'add_combobox_png()', score: 1 },
    { meta: 'Combobox API', caption: 'clear_combobox', value: 'clear_combobox()', score: 1 },
    { meta: 'Combobox API', caption: 'set_combobox_index', value: 'set_combobox_index()', score: 1 },
    { meta: 'Combobox API', caption: 'set_combobox_text', value: 'set_combobox_text()', score: 1 },
    { meta: 'Combobox API', caption: 'get_combobox_text', value: 'get_combobox_text()', score: 1 },
    { meta: 'Grid API', caption: 'grid_cells_set_item_height', value: 'grid_cells_set_item_height()', score: 1 },
    { meta: 'Grid API', caption: 'grid_cells_set_item_width', value: 'grid_cells_set_item_width()', score: 1 },
    { meta: 'Grid API', caption: 'grid_cells_set_content', value: 'grid_cells_set_content()', score: 1 },

    //Module API
    { meta: 'Module API', caption: 'association_event', value: 'association_event()', score: 1 },

    //SlidePage API
    { meta: 'SlidePage API', caption: 'set_scroll_page_size', value: 'set_scroll_page_size()', score: 1 },
    { meta: 'SlidePage API', caption: 'set_scroll_enable', value: 'set_scroll_enable()', score: 1 },
    { meta: 'SlidePage API', caption: 'get_current_page', value: 'get_current_page()', score: 1 },
    { meta: 'SlidePage API', caption: 'set_current_page', value: 'set_current_page()', score: 1 },

    // Meter/Chart APIs
    { meta: 'Meter API', caption: 'create_meter_scale', value: 'create_meter_scale()', score: 1 },
    { meta: 'Meter API', caption: 'meter_add_needle', value: 'meter_add_needle()', score: 1 },
    { meta: 'Meter API', caption: 'meter_set_value', value: 'meter_set_value()', score: 1 },
    { meta: 'Chart API', caption: 'create_curve_record', value: 'create_curve_record()', score: 1 },
    { meta: 'Chart API', caption: 'add_curve_line', value: 'add_curve_line()', score: 1 },

    //Smooth API
    { meta: 'Smooth API', caption: 'create_smooth_slider', value: 'create_smooth_slider()', score: 1 },
    { meta: 'Smooth API', caption: 'set_smooth_slider_pos', value: 'set_smooth_slider_pos()', score: 1 },
    { meta: 'Smooth API', caption: 'get_smooth_slider_pos', value: 'get_smooth_slider_pos()', score: 1 },


    // Message Dialog APIs
    { meta: 'Dialog API', caption: 'create_message', value: 'create_message()', score: 1 },
    { meta: 'Dialog API', caption: 'get_content_id', value: 'get_content_id()', score: 1 },
    { meta: 'Dialog API', caption: 'get_confirm_id', value: 'get_confirm_id()', score: 1 },
    { meta: 'Dialog API', caption: 'get_cancel_id', value: 'get_cancel_id()', score: 1 },

    //Arc API
    { meta: 'Arc API', caption: 'create_arc', value: 'create_arc()', score: 1 },
    { meta: 'Arc API', caption: 'set_arc_pos', value: 'set_arc_pos()', score: 1 },
    { meta: 'Arc API', caption: 'set_acr_range', value: 'set_acr_range()', score: 1 },
    
    // Timer APIs
    { meta: 'Timer API', caption: 'set_main_timer', value: 'set_main_timer()', score: 1 },
    { meta: 'Timer API', caption: 'set_ui_timer', value: 'set_ui_timer()', score: 1 },
    { meta: 'Timer API', caption: 'on_systick_main', value: 'on_systick_main()', score: 1 },
    { meta: 'Timer API', caption: 'on_systick_ui', value: 'on_systick_ui()', score: 1 },
    
    // Position/Size APIs
    { meta: 'Position API', caption: 'align_to', value: 'align_to()', score: 2 },
    { meta: 'Position API', caption: 'align_to_x', value: 'align_to_x()', score: 2 },
    { meta: 'Position API', caption: 'align_to_y', value: 'align_to_y()', score: 2 },
    { meta: 'Position API', caption: 'get_obj_width', value: 'get_obj_width()', score: 1 },
    { meta: 'Position API', caption: 'get_obj_height', value: 'get_obj_height()', score: 1 },
    { meta: 'Position API', caption: 'set_obj_size', value: 'set_obj_size()', score: 1 },
    { meta: 'Position API', caption: 'set_obj_width', value: 'set_obj_width()', score: 1 },
    { meta: 'Position API', caption: 'set_obj_height', value: 'set_obj_height()', score: 1 },
    
    // Property APIs
    { meta: 'Property API', caption: 'is_obj_hidden', value: 'is_obj_hidden()', score: 8 },
    { meta: 'Property API', caption: 'set_obj_hidden', value: 'set_obj_hidden()', score: 9 },
    { meta: 'Property API', caption: 'set_obj_show', value: 'set_obj_show()', score: 9 },
    { meta: 'Property API', caption: 'set_obj_hidden_revease', value: 'set_obj_hidden_revease()', score: 1 },
    { meta: 'Property API', caption: 'set_obj_bk_color', value: 'set_obj_bk_color()', score: 7 },
    { meta: 'Property API', caption: 'set_obj_border_attr', value: 'set_obj_border_attr()', score: 1 },
    { meta: 'Property API', caption: 'set_obj_click_enable', value: 'set_obj_click_enable()', score: 7 },
    { meta: 'Property API', caption: 'del_obj', value: 'del_obj()', score: 1 },
    
    // Password/Warning APIs
    { meta: 'Password API', caption: 'check_password', value: 'check_password()', score: 7 },
    { meta: 'Password API', caption: 'modify_password', value: 'modify_password()', score: 7 },
    { meta: 'Password API', caption: 'on_password_result', value: 'on_password_result()', score: 7 },
    { meta: 'Password API', caption: 'set_confirm', value: 'set_confirm()', score: 1 },
    { meta: 'Password API', caption: 'set_cancel', value: 'set_cancel()', score: 1 },
    { meta: 'Password API', caption: 'set_password_input', value: 'set_password_input()', score: 1 },
    { meta: 'Password API', caption: 'set_out_range', value: 'set_out_range()', score: 1 },
    { meta: 'Password API', caption: 'set_password_error', value: 'set_password_error()', score: 1 },
    { meta: 'Password API', caption: 'set_modify_password1', value: 'set_modify_password1()', score: 1 },
    { meta: 'Password API', caption: 'set_modify_password2', value: 'set_modify_password2()', score: 1 },
    { meta: 'Password API', caption: 'set_modify_tishi1', value: 'set_modify_tishi1()', score: 1 },
    { meta: 'Password API', caption: 'set_modify_tishi2', value: 'set_modify_tishi2()', score: 1 },
    { meta: 'Password API', caption: 'set_modify_tishi3', value: 'set_modify_tishi3()', score: 1 },
    { meta: 'Password API', caption: 'set_modify_tishi4', value: 'set_modify_tishi4()', score: 1 },




    { meta: 'Warning API', caption: 'set_warnning_mode', value: 'set_warnning_mode()', score: 5 },
    { meta: 'Warning API', caption: 'add_warnning_condition', value: 'add_warnning_condition()', score: 5 },
    { meta: 'Warning API', caption: 'add_warnning_bit_condition', value: 'add_warnning_bit_condition()', score: 5 },
    { meta: 'Warning API', caption: 'get_warnning_list_size', value: 'get_warnning_list_size()', score: 1 },
    { meta: 'Warning API', caption: 'get_warnning_list', value: 'get_warnning_list()', score: 1 },
    { meta: 'Warning API', caption: 'clear_warnning_list', value: 'clear_warnning_list()', score: 1 },

    //Attr API
    { meta: 'Attr API', caption: 'set_obj_attr_ext1', value: 'set_obj_attr_ext1()', score: 5 },
    { meta: 'Attr API', caption: 'get_obj_attr_ext1', value: 'get_obj_attr_ext1()', score: 5 },
    { meta: 'Attr API', caption: 'set_obj_attr_ext2', value: 'set_obj_attr_ext2()', score: 4 },
    { meta: 'Attr API', caption: 'get_obj_attr_ext2', value: 'get_obj_attr_ext2()', score: 4 },
    { meta: 'Attr API', caption: 'set_obj_attr_ext3', value: 'set_obj_attr_ext3()', score: 3 },
    { meta: 'Attr API', caption: 'get_obj_attr_ext3', value: 'get_obj_attr_ext3()', score: 3 },
    { meta: 'Attr API', caption: 'set_obj_attr_ext4', value: 'set_obj_attr_ext4()', score: 2 },
    { meta: 'Attr API', caption: 'get_obj_attr_ext4', value: 'get_obj_attr_ext4()', score: 2 },
    { meta: 'Attr API', caption: 'set_obj_attr_ext5', value: 'set_obj_attr_ext5()', score: 1 },
    { meta: 'Attr API', caption: 'get_obj_attr_ext5', value: 'get_obj_attr_ext5()', score: 1 },
    { meta: 'Attr API', caption: 'set_obj_attr_ext6', value: 'set_obj_attr_ext6()', score: 1 },
    { meta: 'Attr API', caption: 'get_obj_attr_ext6', value: 'get_obj_attr_ext6()', score: 1 },
    { meta: 'Attr API', caption: 'set_obj_attr_ext7', value: 'set_obj_attr_ext7()', score: 1 },
    { meta: 'Attr API', caption: 'get_obj_attr_ext7', value: 'get_obj_attr_ext7()', score: 1 },
    { meta: 'Attr API', caption: 'set_obj_attr_ext8', value: 'set_obj_attr_ext8()', score: 1 },
    { meta: 'Attr API', caption: 'get_obj_attr_ext8', value: 'get_obj_attr_ext8()', score: 1 },

    //List APIS
    { meta: 'List API', caption: 'add_ext_list', value: 'add_ext_list()', score: 1 },
    { meta: 'List API', caption: 'clear_ext_list', value: 'clear_ext_list()', score: 1 },
    { meta: 'List API', caption: 'get_ext_list_size', value: 'get_ext_list_size()', score: 1 },
    { meta: 'List API', caption: 'get_ext_info', value: 'get_ext_info()', score: 1 },

    //Buz APIs
    { meta: 'Buz API', caption: 'set_buz_enable', value: 'set_buz_enable()', score: 5 },
    { meta: 'Buz API', caption: 'get_buz_enable', value: 'get_buz_enable()', score: 5 },
    { meta: 'Buz API', caption: 'set_buz_time', value: 'set_buz_time()', score: 4 },
    
    // Data Trigger APIs
    { meta: 'Trigger API', caption: 'add_user_value_condition', value: 'add_user_value_condition()', score: 1 },
    { meta: 'Trigger API', caption: 'add_user_value_condition_bit', value: 'add_user_value_condition_bit()', score: 1 },
    { meta: 'Trigger API', caption: 'add_modbus_value_condition', value: 'add_modbus_value_condition()', score: 1 },
    { meta: 'Trigger API', caption: 'add_modbus_value_condition_bit', value: 'add_modbus_value_condition_bit()', score: 1 },
    
    //Schedule APIS
    { meta: 'Schedule API', caption: 'clear_scheduled_task', value: 'clear_scheduled_task()', score: 1 },
    { meta: 'Schedule API', caption: 'set_scheduled_task_paramter', value: 'set_scheduled_task_paramter()', score: 1 },
    
    // Audio APIs
    { meta: 'Audio API', caption: 'play_sound', value: 'play_sound()', score: 1 },
    { meta: 'Audio API', caption: 'stop_play_sound_id', value: 'stop_play_sound_id()', score: 1 },
    { meta: 'Audio API', caption: 'stop_play_sound_all', value: 'stop_play_sound_all()', score: 1 },
    { meta: 'Audio API', caption: 'get_sound_voice', value: 'get_sound_voice()', score: 1 },
    { meta: 'Audio API', caption: 'set_sound_voice', value: 'set_sound_voice()', score: 1 },
    
    // RTC Time APIs
    { meta: 'Time API', caption: 'update_time_label', value: 'update_time_label()', score: 1 },
    { meta: 'Time API', caption: 'switch_timestamp_time', value: 'switch_timestamp_time()', score: 1 },
    { meta: 'Time API', caption: 'get_cur_time', value: 'get_cur_time()', score: 1 },
    { meta: 'Time API', caption: 'get_cur_timestamp', value: 'get_cur_timestamp()', score: 1 },
    { meta: 'Time API', caption: 'get_cur_year', value: 'get_cur_year()', score: 1 },
    { meta: 'Time API', caption: 'get_cur_month', value: 'get_cur_month()', score: 1 },
    { meta: 'Time API', caption: 'get_cur_day', value: 'get_cur_day()', score: 1 },
    { meta: 'Time API', caption: 'get_cur_hour', value: 'get_cur_hour()', score: 1 },
    { meta: 'Time API', caption: 'get_cur_minute', value: 'get_cur_minute()', score: 1 },
    { meta: 'Time API', caption: 'set_time', value: 'set_time()', score: 1 },
    { meta: 'Time API', caption: 'set_time_single', value: 'set_time_single()', score: 1 },
    
    // Language APIs
    { meta: 'Language API', caption: 'set_language', value: 'set_language()', score: 8 },
    { meta: 'Language API', caption: 'get_language', value: 'get_language()', score: 8 },
    
    // Upgrade/Export APIs
    { meta: 'System API', caption: 'get_base_app_version', value: 'get_base_app_version()', score: 2 },
    { meta: 'System API', caption: 'app_upgrade_exist', value: 'app_upgrade_exist()', score: 1 },
    { meta: 'System API', caption: 'app_upgrade', value: 'app_upgrade()', score: 1 },
    { meta: 'System API', caption: 'start_upgrade', value: 'start_upgrade()', score: 1 },
    { meta: 'System API', caption: 'clear_upgrade_id', value: 'clear_upgrade_id()', score: 1 },
    { meta: 'System API', caption: 'start_upgrade_tishi', value: 'start_upgrade_tishi()', score: 1 },
    { meta: 'System API', caption: 'export_data', value: 'export_data()', score: 1 },

    //Thread APIs
    { meta: 'Thread API', caption: 'add_lua_thread', value: 'add_lua_thread()', score: 1 },
    { meta: 'Thread API', caption: 'get_lua_thread_status', value: 'get_lua_thread_status()', score: 1 },
    { meta: 'Thread API', caption: 'sleep_s', value: 'sleep_s()', score: 1 },
    { meta: 'Thread API', caption: 'sleep_ms', value: 'sleep_ms()', score: 1 },

    //Scroll APIs
    { meta: 'Scroll API', caption: 'create_scroll_saver', value: 'create_scroll_saver()', score: 1 },
    { meta: 'Scroll API', caption: 'is_have_touch', value: 'is_have_touch()', score: 1 },

    //Toast API
    { meta: 'Toast API', caption: 'create_toast_show', value: 'create_toast_show()', score: 9 },

    //NetWork APIs
    { meta: 'NetWork API', caption: 'get_wifi_mac', value: 'get_wifi_mac()', score: 1 },
    { meta: 'NetWork API', caption: 'set_wifi_mac', value: 'set_wifi_mac()', score: 1 },
    { meta: 'NetWork API', caption: 'get_wifi_state', value: 'get_wifi_state()', score: 1 },
    { meta: 'NetWork API', caption: 'get_network_state', value: 'get_network_state()', score: 1 },
    { meta: 'NetWork API', caption: 'get_string_crc', value: 'get_string_crc()', score: 1 },
    { meta: 'NetWork API', caption: 'get_4G_imei', value: 'get_4G_imei()', score: 1 },
    { meta: 'NetWork API', caption: 'get_4G_ccid', value: 'get_4G_ccid()', score: 1 },
    { meta: 'NetWork API', caption: 'get_4g_status', value: 'get_4g_status()', score: 1 },
    { meta: 'NetWork API', caption: 'set_4G_imei', value: 'set_4G_imei()', score: 1 },
    { meta: 'NetWork API', caption: 'wifi_ssid_size', value: 'wifi_ssid_size()', score: 1 },
    { meta: 'NetWork API', caption: 'wifi_scan_ssid', value: 'wifi_scan_ssid()', score: 1 },
    { meta: 'NetWork API', caption: 'get_wifi_ssid', value: 'get_wifi_ssid()', score: 1 },
    { meta: 'NetWork API', caption: 'connect_wifi', value: 'connect_wifi()', score: 1 },
    { meta: 'NetWork API', caption: 'reconnect_wifi', value: 'reconnect_wifi()', score: 1 },
    { meta: 'NetWork API', caption: 'wifi_select', value: 'wifi_select()', score: 1 },
    { meta: 'NetWork API', caption: 'wifi_password', value: 'wifi_password()', score: 1 },
    { meta: 'NetWork API', caption: 'set_connect_network_info', value: 'set_connect_network_info()', score: 1 },
    { meta: 'NetWork API', caption: 'mqtt_connect', value: 'mqtt_connect()', score: 1 },
    { meta: 'NetWork API', caption: 'mqtt_subscribe', value: 'mqtt_subscribe()', score: 1 },
    { meta: 'NetWork API', caption: 'mqtt_publish', value: 'mqtt_publish()', score: 1 },
    { meta: 'NetWork API', caption: 'network_send_string_package', value: 'network_send_string_package()', score: 1 },
    { meta: 'NetWork API', caption: 'network_send_bytes_package', value: 'network_send_bytes_package()', score: 1 },
    { meta: 'NetWork API', caption: 'server_get_request', value: 'server_get_request()', score: 1 },
    { meta: 'NetWork API', caption: 'server_post_request', value: 'server_post_request()', score: 1 },
    { meta: 'NetWork API', caption: 'server_download_file', value: 'server_download_file()', score: 1 },
    { meta: 'NetWork API', caption: 'on_server_notify', value: 'on_server_notify()', score: 1 },

    //Sort APIs
    { meta: 'Sort API', caption: 'clear_sort_info', value: 'clear_sort_info()', score: 6 },
    { meta: 'Sort API', caption: 'add_sort_info', value: 'add_sort_info()', score: 6 },
    { meta: 'Sort API', caption: 'get_sort_enable_count', value: 'get_sort_enable_count()', score: 6 },
    { meta: 'Sort API', caption: 'get_sort_address_info', value: 'get_sort_address_info()', score: 6 },

    //Data APIs
    { meta: 'Data API', caption: 'switch_negative_to_u16', value: 'switch_negative_to_u16()', score: 1 },
    { meta: 'Data API', caption: 'switch_u16_to_negative', value: 'switch_u16_to_negative()', score: 1 },
    
    // Image Selector API
    { meta: 'Image API', caption: 'create_png_select', value: 'create_png_select()', score: 1 }
        
        //TODO: 待补充！！
      ]
      let langTools = ace.require('ace/ext/language_tools')
      langTools.addCompleter({
        getCompletions: function (editor, session, pos, prefix, callback) {
          if (prefix.length === 0) {
            return callback(null, [])
          } else {
            return callback(null, acData)
          }
        }
      })
    },

    setJsonMode() {
      this.aceEditor.getSession().setMode('ace/mode/json')
    },

    setCssMode() {
      this.aceEditor.getSession().setMode('ace/mode/css')
    },

    setLuaMode() {
      this.aceEditor.getSession().setMode('ace/mode/lua')
    },

    setJsMode() {
      this.aceEditor.getSession().setMode('ace/mode/javascript')
    },

    getEditorAnnotations() {
      return this.aceEditor.getSession().getAnnotations()
    },

    setValue(newValue) {
      this.aceEditor.getSession().setValue(newValue)
    }
  }
}
</script>

<style lang="scss" scoped>
.ace-editor {
  min-height: 400px;
  height: 700px;
}
</style>
